package com.manager.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;

public class SendEmail {

    public  Integer emailRandomSend(String email) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("mail-config.xml");
        //获得邮件发送者对象
        JavaMailSender sender = (JavaMailSender)ac.getBean("mailSender");
        //创建邮件消息对象
        MimeMessage mime = sender.createMimeMessage();
        //如果发送失败，则验证码为111111
        Integer a=111111;
        try {
            //设置邮件信息对象
            MimeMessageHelper helper = new MimeMessageHelper(mime,true,"utf-8");
            //设置邮件发送者
            helper.setFrom("javalearningg@163.com");
            //设置邮件接收者
            helper.setTo(email);
            System.out.println(email);
            //设置邮件主题
            helper.setSubject("修改密码验证");

            a=new Random().nextInt(100000);
            //设置邮件内容
            helper.setText("您本次修改密码所需的验证码是："+a);
            //发送邮件
            sender.send(mime);
            //System.out.println("邮件发送成功...");
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return a;
    }


}
